package com.songshuang.myutilsboot.stlf.core;

import com.songshuang.myutilsboot.stlf.io.Constants;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

final class ModelIO {
    private ModelIO() {}

    static void readModel(String modelVersion, AnnPredictor.AnnConfig cfg) throws Exception {
        File f = new File(Constants.ANN_MODEL_PATH + modelVersion + ".txt");
        try (BufferedReader br = new BufferedReader(new FileReader(f))) {
            // 跳过头部，定位到各段报告，与C++的posdata/getline等价
            posTo(br, "W Report");
            // 跳过一行分隔线
            br.readLine();
            // 读取 W: xnum 行，每行 hnum 列
            for (int i = 1; i <= cfg.xnum; i++) {
                String line = br.readLine();
                if (line == null) throw new IllegalStateException("模型文件格式错误: W");
                String[] parts = line.trim().split("\\s+");
                int idx = 0;
                for (int j = 1; j <= cfg.hnum; j++) {
                    if (idx >= parts.length) throw new IllegalStateException("W 长度不足");
                    cfg.W[i][j] = Double.parseDouble(parts[idx++]);
                }
            }
            posTo(br, "V Report");
            br.readLine();
            for (int i = 1; i <= cfg.hnum; i++) {
                String line = br.readLine();
                if (line == null) throw new IllegalStateException("模型文件格式错误: V");
                String[] parts = line.trim().split("\\s+");
                int idx = 0;
                for (int j = 1; j <= cfg.ynum; j++) {
                    if (idx >= parts.length) throw new IllegalStateException("V 长度不足");
                    cfg.V[i][j] = Double.parseDouble(parts[idx++]);
                }
            }
            posTo(br, "Hth Report");
            br.readLine();
            String lineH = br.readLine();
            if (lineH == null) throw new IllegalStateException("模型文件格式错误: Hth");
            String[] hParts = lineH.trim().split("\\s+");
            for (int i = 1; i <= cfg.hnum; i++) cfg.hth[i] = Double.parseDouble(hParts[i-1]);

            posTo(br, "Yth Report");
            br.readLine();
            String lineY = br.readLine();
            if (lineY == null) throw new IllegalStateException("模型文件格式错误: Yth");
            String[] yParts = lineY.trim().split("\\s+");
            for (int i = 1; i <= cfg.ynum; i++) cfg.yth[i] = Double.parseDouble(yParts[i-1]);
        }
    }

    private static void posTo(BufferedReader br, String marker) throws Exception {
        String line;
        while ((line = br.readLine()) != null) {
            if (line.contains(marker)) return;
        }
        throw new IllegalStateException("未找到标记: " + marker);
    }

    static void writeModel(String modelVersion, AnnPredictor.AnnConfig cfg) throws Exception {
        java.io.File dir = new java.io.File(Constants.ANN_MODEL_PATH);
        if (!dir.exists()) dir.mkdirs();
        java.io.File f = new java.io.File(dir, modelVersion + ".txt");
        try (java.io.PrintWriter pw = new java.io.PrintWriter(new java.io.OutputStreamWriter(new java.io.FileOutputStream(f), java.nio.charset.StandardCharsets.UTF_8))) {
            String now = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
            pw.println("Start_time=" + now);
            pw.println("End_time  =" + now);
            pw.println("----------------------------------------");
            pw.println(" xnum = " + cfg.xnum);
            pw.println(" hnum = " + cfg.hnum);
            pw.println(" ynum = " + cfg.ynum);
            pw.println("----------------------------------------");
            // W Report
            pw.println("W Report");
            pw.println("----------------------------------------");
            for (int i = 1; i <= cfg.xnum; i++) {
                StringBuilder sb = new StringBuilder();
                for (int j = 1; j <= cfg.hnum; j++) {
                    if (j > 1) sb.append(' ');
                    sb.append(cfg.W[i][j]);
                }
                pw.println(sb.toString());
            }
            pw.println("----------------------------------------");
            // V Report
            pw.println("V Report");
            pw.println("----------------------------------------");
            for (int i = 1; i <= cfg.hnum; i++) {
                StringBuilder sb = new StringBuilder();
                for (int j = 1; j <= cfg.ynum; j++) {
                    if (j > 1) sb.append(' ');
                    sb.append(cfg.V[i][j]);
                }
                pw.println(sb.toString());
            }
            pw.println("----------------------------------------");
            // Hth Report
            pw.println("Hth Report");
            pw.println("----------------------------------------");
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 1; i <= cfg.hnum; i++) {
                    if (i > 1) sb.append(' ');
                    sb.append(cfg.hth[i]);
                }
                pw.println(sb.toString());
            }
            pw.println("----------------------------------------");
            // Yth Report
            pw.println("Yth Report");
            pw.println("----------------------------------------");
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 1; i <= cfg.ynum; i++) {
                    if (i > 1) sb.append(' ');
                    sb.append(cfg.yth[i]);
                }
                pw.println(sb.toString());
            }
            pw.flush();
        }
    }
}


